<!--
 * @ Author: 李皓田
 * @ Create Time: 2022-06-03 19:33:15
 * @ Modified by: 李皓田
 * @ Modified time: 2022-06-30 21:55:22
 * @ Description:
 -->


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽事件</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        ul{
            display: flex;
            position: relative;
        }

        li{
            list-style: none;
            float: left;
            width: 200px;
            margin: 15px;
        }

        li img{
            width: 200px;
        }

        p{
            height: 30px;
            line-height: 30px;
            font-weight: bold;
            border-bottom: 1px #333 dashed;
        }

        #div1{
            width: 800px;
            height: 400px;
            border: 1px #000 solid;
        }

        .box1{
            width: 300px;
            float: left;
        }

        .box2{
            width: 30px;
            float: left;

        }

        .box3{
            width: 300px;
            float: left;
            margin-left: 100px;
        }
    </style>
</head>
<body>
    <ul>
        <li draggable="true">
            <img src="1.jpg"/>
            <p>JavaScript语言精粹</p>
            <p>40￥</p>
        </li>
        <li draggable="true">            
            <img src="2.jpg"/>
            <p>JavaScript权威指南</p>
            <p>40￥</p>
        </li>
        <li draggable="true">
            <img src="3.jpg"/>
            <p>精通JavaScript</p>
            <p>40￥</p>
        </li>
        <li draggable="true">            
            <img src="4.jpg"/>
            <p>DOM编程艺术</p>
            <p>40￥</p>
        </li>
    </ul>
    <div id="div1"></div>
    <script>
        window.onload = function(){
            var li = document.querySelectorAll("li");
            var div = document.getElementById("div1");
            var num;//计数 书的册数

            var obj = {};

            for(var i = 0; i < li.length; i++){

                //开始拖拽时触发
                li[i].ondragstart = function(e){

                    var pData = this.getElementsByTagName("p");

                    //获取书名和价格，并传递该数据
                    e.dataTransfer.setData('title',pData[0].innerHTML);
                    e.dataTransfer.setData('money',pData[1].innerHTML);

                    //console.log(e);
                }
            }

            //默认情况，浏览器会执行该事件，但该事件没完成任何事情
            div.ondragover = function(e){
                e.preventDefault();
            }

            //放置在目标位置触发
            div.ondrop = function(e){
                //阻止浏览器的默认行为
                e.preventDefault();
                //获取书名
                var  title = e.dataTransfer.getData('title');
                var  money = e.dataTransfer.getData('money');

                console.log(title);

                //把书名作为键
                if(!obj[title]){
                    var p = document.createElement("p");
                    var span = document.createElement("span");
                    span.className = "box1";
                    span.innerHTML = title;
                    p.appendChild(span);

                    span = document.createElement("input");
                    span.className = "box2";
                    span.value = 1;
                    span.type = "number"
                    p.appendChild(span);

                    span = document.createElement("span");
                    span.className = "box3";
                    span.innerHTML = money;
                    p.appendChild(span);

                    div.appendChild(p);
                    obj[title] = 1;
                }else{
                    var box1 = document.getElementsByClassName("box1");
                    var box2 = document.getElementsByClassName("box2");

                    for(var i = 0;i<box1.length;i++){
                        if(box1[i].innerHTML == title){
                            box2[i].value = parseInt(box2[i].value) + 1;
                        }
                    }
                }

                console.log(obj);   

            }

            //计算所有书的总价,每本书的总价
        
        }

    </script>
</body>
</html>